<?php

/**
 * Project: CrazyPoems
 * Author: Alexandru
 // Traslate By: Christian :P
 * Date: 22/10/2008
 * File: poem.php
 */

// Load default settings.
require('inc/config.php');
require('inc/functions.misc.php');
require('inc/functions.index.php');
require('inc/functions.poem.php');
require('inc/functions.member.php');
include_once 'inc/securimage/securimage.php';
$securimage = new Securimage();

// Get poem info.
$poemId = $_GET['id'];
$delete = $_GET['delete'];
$vote = $_GET['vote'];
$error_msg = "";
$fullname = isset($_POST['fullname']) && strlen($_POST['fullname']) > 0 ? $_POST['fullname'] : '';
$email = isset($_POST['email']) && strlen($_POST['email']) > 0 ? $_POST['email'] : '';
$comment = isset($_POST['comment']) && strlen($_POST['comment']) > 0 ? $_POST['comment'] : '';

$q = mysql_query("SELECT * FROM " . TABLE_POEMS . " WHERE id = $poemId") or die(mysql_error());
$r = mysql_fetch_assoc($q);
if ($r) {
  // Add comment.
  if (isset($_POST['form_sent']) && $_POST['form_sent'] == "yes") {
    if (strlen($fullname) == 0) {
      $error_msg .= "<p>Introdu numele.</p>";
    }
    if (strlen($email) == 0) {
      $error_msg .= "<p>Introdu e-mail.</p>";
    } else {
      if (!preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\.\\+=_-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $email)) {
        $error_msg .= "<p>Adresa de e-mail nu este valida.</p>";
      }
    }
    if (strlen($comment) == 0) {
      $error_msg .= "<p>Ai uitat sa comentezi!</p>";
    }
    // Check if anonymous comments are enabled.
    if (ANONIMOUS_COMMENTS_DISABLED && strlen($cookie->username) == 0) {
      $error_msg .= "<p>Comentariile anonime sunt dezactivate momentan, pentru a pune un comentariu trebuie <a href=\"".BASE_URL."new-member.php\">sa te inregistrezi</a>.</p>";
    }
    if (strlen($cookie->username) == 0) {
      if ($securimage->check($_POST['captcha_code']) == false) {
       $error_msg .= "<p>".l('El código de verificación introducido es incorrecto.')."</p>";
      }
    }

    // Insert comment and notify author.
    if (strlen($error_msg) == 0) {
      Hop_addComment($poemId, $r['author'], $r['title'] , $fullname, $email, $comment);
    }
    $smarty->assign('fullname', $fullname);
    $smarty->assign('email', $email);
    $smarty->assign('comment', $comment);
    $smarty->assign('form_sent', $_POST['form_sent']);

  }
  // Add vote.
  if (strlen($vote) > 0) {
    // Redirect if user not logged in.
    if (isset($cookie->username) && strlen($cookie->username) > 0) {
      Hop_addVote($poemId);
    } else {
      header("Location: ".BASE_URL."login.php?next=".get_full_url());
    }
  }
  // Check if user voted.
  $smarty->assign('voted', Hop_checkVote($poemId));
  // Get poem.
  $q2 = mysql_query("UPDATE " . TABLE_POEMS . " SET readed = readed + 1 WHERE id = $poemId") or die(mysql_error());
  $smarty->assign('id', $r['id']);
  $smarty->assign('title', $r['title']);
  $smarty->assign('poem_url', get_link('poem', array('id' => $r['id'], 'title' => $r['title'], 'category' => $r['category'])));
  $smarty->assign('lang', $r['lang']);
  $smarty->assign('category', $r['category']);
  $categoryName = getCategoryById($r['category']);
  $smarty->assign('categoryName', $categoryName);
  $smarty->assign('poem', nl2br($r['poem']));
  $smarty->assign('publishDate', parseDate($r['publish_date']));
  $smarty->assign('lastUpdate', parseDate($r['last_update']));
  $smarty->assign('author', $r['author']);
  $smarty->assign('readed', $r['readed']+1);
  $smarty->assign('pageTitle', $r['title']);
  $breadcrumbs = '<a href="'.BASE_URL.'">'.l('Inicio').'</a> &raquo; <a href="'.BASE_URL.'poems.php">'.l('Poesías').'</a> &raquo; ';
  $breadcrumbs .= '<a href="'.BASE_URL.'poems.php?category='.$r['category'].'">'.$categoryName.'</a> &raquo; ';
  $breadcrumbs .= $r['title'];
  $smarty->assign('breadcrumbs', $breadcrumbs);
  // Get comments.
  $smarty->assign('comments', Hop_getComments($poemId));
  // Get votes.
  $smarty->assign('votes', Hop_countVotes($poemId));
  // Get poems from same user.
  $smarty->assign('sameAuthorPoems', Hop_sameAuthorPoems($r['author'], $r['id']));
  // Get response poems.
  $smarty->assign('responsePoems', Hop_getResponsePoems($poemId));
  // Is this poem a response? If yes, get the original.
  if (strlen($r['response_to']) > 0) {
    $response_to = Hop_getPoem($r['response_to']);
    $response_to['link'] = get_link('poem', array('id' => $response_to['id'],
            'title' => $response_to['title'],
            'category' => $response_to['category']));
    $smarty->assign('responseTo', $response_to);
  }
} else {
  $smarty->assign('pageTitle', 'Aceasta poezie nu exista.');
  $smarty->assign('unknownPoem', 'yes');
}

// Delete poem?
if (strlen($delete) > 0) {
  if ($cookie->username == $r['author'] || $cookie->admin == 1) {
    $q3 = mysql_query("DELETE FROM " . TABLE_POEMS . " WHERE id = $poemId") or die(mysql_error());
    $q4 = mysql_query("DELETE FROM " . TABLE_COMMENTS . " WHERE poem_id = $poemId") or die(mysql_error());
    $q5 = mysql_query("UPDATE " . TABLE_POEMS . " SET response_to = NULL WHERE response_to = $poemId") or die(mysql_error());
    $smarty->assign('deleted', "yes");
  }
}

$smarty->assign('error_msg', $error_msg);

// Go!
$smarty->display('poem.tpl');

// End page.
require('inc/end.php');
?>
